import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/wroks/order_item_info.dart';
import 'package:app/src/http/bean/wroks/pay_notice_bean.dart';
import 'package:app/src/http/bean/wroks/product_bean.dart';
import 'package:app/src/http/bean/wroks/receipt_bean.dart';
import 'package:app/src/http/bean/wroks/special_clauses.dart';
import 'package:app/src/ui/dialog/dailog.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/a_combined_utils.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../app_ui.dart';

///开票审核，申请条目
class UiReceiptItemInfo {
  static Widget build(BuildContext context, List<ReceiptItem>? data,
      {String? title}) {
    if (StringUtils.isEmpty(data)) {
      return AppUi.getEmptyView();
    }
    List<Widget> children = [];
    if (StringUtils.isEmpty(title)) {
      title = AppStrings.of(context).ApplyEntry; //申请条目
    }
    children.add(
        UiBase.title(context, title, bottom: AppEdgeInsets.margin_bottom)); //标题
    children.add(_table(
      context,
      data,
    ));
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  static Widget _table(
    BuildContext context,
    List<ReceiptItem>? data,
  ) {
    return StatefulBuilder(
      builder: (BuildContext context, StateSetter setState) {
        List<TableRow> children = [];
        //==========================================================================标题
        children.add(TableRow(
            //第一行，添加背景色
            decoration:
                BoxDecoration(color: AppColors.backgroundColorWithTableTitle),
            children: _titles(context)));
        if (data != null && data.length > 0) {
          var postion = 1;
          data.forEach((element) {
            //======================================================================内容
            var cells = _contents(context, postion, element, setState);
            postion++;
            children.add(TableRow(children: cells,decoration: BoxDecoration(
                color: AppColors.getColorWithTableRow(postion))));
          });
        }

        ///合计
        if (data != null) {
          children.add(TableRow(
              //最后一行，添加背景色
              decoration:
                  BoxDecoration(color: AppColors.backgroundColorWithTableAcom),
              children: _aCombined(context, data!)));
        }
        var table = Table(
          defaultVerticalAlignment: TableCellVerticalAlignment.middle,
          columnWidths: {
            0: FixedColumnWidth(50),
            1: FixedColumnWidth(100),
            5: FixedColumnWidth(180),
            6: FixedColumnWidth(180),
            7: FixedColumnWidth(200),
          },
          border: AppStyle.getTableBorder(),
          textDirection: TextDirection.ltr,
          children: children,
        );
        var width = 1200.0;
        if (width < px.screenWidth()) {
          width = px.screenWidth();
        }
        return SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          physics: BouncingScrollPhysics(),
          child: Container(
            width: width,
            child: table,
          ),
        );
      },
    );
  }

  ///标题
  static List<Widget> _titles(BuildContext context) {
    List<Widget> children = [];
    children.add(_addItem(AppStrings.of(context).TheSerial)); //序号
    children.add(_addItem(AppStrings.of(context).SumNum)); //数量（片）
    children.add(_addItem(AppStrings.of(context).SumArea)); //面积（m2）
    children.add(_addItem(AppStrings.of(context).SumWeight)); //重量（kg）
    children.add(_addItem(AppStrings.of(context).Amount6)); //金额（元）
    children.add(_addItem(AppStrings.of(context).OtherAmount2)); //特殊加工费（元）
    children.add(_addItem(AppStrings.of(context).AfterAmount)); //减免后金额（元）
    children
        .add(_addItem(AppStrings.of(context).AfterOtherAmount)); //减免后特殊加工费（元）
    children.add(_addItem(AppStrings.of(context).StatementCode)); //对账单号
    return children;
  }

  ///内容
  static List<Widget> _contents(BuildContext context, int postion,
      ReceiptItem element, StateSetter setState) {
    List<Widget> children = [];
    children.add(_addItem((postion).toString())); //序号
    children.add(_addItem(element.SumNum)); //数量（片）
    children.add(_addItem(element.SumArea)); //面积（m2）
    children.add(_addItem(element.SumWeight)); //重量（kg）
    children.add(_addItem(element.Amount)); //金额（元）
    children.add(_addItem(element.OtherAmount)); //特殊加工费（元）
    children.add(_addItem(element.AfterAmount)); //减免后金额（元）
    children.add(_addItem(element.AfterOtherAmount)); //减免后特殊加工费
    children.add(_addItem(element.StatementCode)); //对账单号
    return children;
  }

  ///合计
  static List<Widget> _aCombined(
      BuildContext context, List<ReceiptItem> datas) {
    List<Widget> children = [];
    children.add(_addItem(AppStrings.of(context).ACombined)); //合计
    double SumNum = 0;
    datas?.forEach((element) {
      var str = element.SumNum;
      SumNum = AcombinedUtils.add(SumNum, str);
    });
    children.add(_addItem(AcombinedUtils.getNum(SumNum))); //fixme 数量（片）
    double SumArea = 0;
    datas?.forEach((element) {
      var str = element.SumArea;
      SumArea = AcombinedUtils.add(SumArea, str);
    });
    children.add(_addItem(AcombinedUtils.getArea4(SumArea))); //fixme 面积（m2）
    double SumWeight = 0;
    datas?.forEach((element) {
      var str = element.SumWeight;
      SumWeight = AcombinedUtils.add(SumWeight, str);
    });
    children.add(_addItem(AcombinedUtils.getWeight3(SumWeight))); //fixme 重量（kg）
    double Amount = 0;
    datas?.forEach((element) {
      var str = element.Amount;
      Amount = AcombinedUtils.add(Amount, str);
    });
    children.add(_addItem(AcombinedUtils.getMoney(Amount))); //fixme 金额（元）
    double OtherAmount = 0;
    datas?.forEach((element) {
      var str = element.OtherAmount;
      OtherAmount = AcombinedUtils.add(OtherAmount, str);
    });
    children
        .add(_addItem(AcombinedUtils.getMoney(OtherAmount))); //fixme 特殊加工费（元）
    double AfterAmount = 0;
    datas?.forEach((element) {
      var str = element.AfterAmount;
      AfterAmount = AcombinedUtils.add(AfterAmount, str);
    });
    children
        .add(_addItem(AcombinedUtils.getMoney(AfterAmount))); //fixme 减免后金额（元）
    double AfterOtherAmount = 0;
    datas?.forEach((element) {
      var str = element.AfterOtherAmount;
      AfterOtherAmount = AcombinedUtils.add(AfterOtherAmount, str);
    });
    children.add(_addItem(
        AcombinedUtils.getMoney(AfterOtherAmount))); //fixme 减免后特殊加工费（元）
    children.add(_addItem("")); //对账单号
    return children;
  }

  static Widget _addItem(String? text,
      {bool isBlue = false, GestureTapCallback? onTap}) {
    Widget child = Center(child: _text(text, isBlue: isBlue));
    if (onTap == null) {
      return child;
    }
    return InkWell(
      child: child,
      onTap: onTap,
    );
  }

  static Widget _text(String? text, {bool isBlue = false}) {
    TextStyle? style = null;
    if (isBlue) {
      style = AppStyle.textStyleWithCardBlue();
    } else {
      style = AppStyle.textStyleWithCard();
    }
    return Text(
      text?.trim() ?? "",
      style: style,
    );
  }
}
